package controllers

import (
	"github.com/astaxie/beego"
	"travelservia/models"
)

type YoujiController struct {
	beego.Controller
}
func (c *YoujiController) Prepare()  {
	c.Data["headlink"]="youji"
}

func (c *YoujiController)GetList() {
	typestr := c.GetString("type")
	tacleYoujiSearch(&c.Controller)
	youjis := []models.Youji{}
	counts, err := models.GetYoujiListCount()
	if err != nil {
		Message(&c.Controller, `/`, "获取列表错误即将返回首页", "", false)
	}
	numPerPage := 40
	p := SetPaginator(&c.Controller, numPerPage, counts)
	CurrentPage := p.Page()

	if typestr == "hot" {
		models.GetYoujiHotList(&youjis, (CurrentPage - 1) * numPerPage, numPerPage)
	}else if typestr=="newest"{
		models.GetYoujiList(&youjis, (CurrentPage - 1) * numPerPage, numPerPage)
	}else{
		models.GetYoujiList(&youjis, (CurrentPage - 1) * numPerPage, numPerPage)
	}
	c.Data["youji"]=&youjis
	c.Data["listpage"]=true
	c.TplName="youjilist.html"
}
func tacleYoujiSearch(c *beego.Controller) {
	search:=c.GetString("search")
	if search==""{
		return
	}
	youjis := []models.Youji{}
	counts, err := models.GetYoujiSearchCount(search)
	if err != nil {
		Message(c, `/`, "获取列表错误即将返回首页", "", false)
	}
	numPerPage := 40
	p := SetPaginator(c, numPerPage, counts)
	CurrentPage := p.Page()
	models.GetYoujiHotList(&youjis, (CurrentPage - 1) * numPerPage, numPerPage)
	c.Data["youji"]=&youjis
	c.TplName="youjilist.html"
	c.Render()
	c.StopRun()
}

func (c *YoujiController)GetDetail()  {
	id,_:=c.GetInt("id")
	youji:=models.Youji{Id:id}
	if err:=models.GetYoujiDetail(&youji);err!=nil{
		Message(&c.Controller, `/`, "获取列表错误即将返回首页", "", false)
	}
	c.Data["youji"]=&youji
	c.TplName="youjidetail.html"
}

func (c *YoujiController)AddBrowseCount() {
	id,_:=c.GetInt("id")
	if id==0{
		Json_Message(&c.Controller,1,"id not right")
	}
	youji:=models.Youji{Id:id}
	if err:=models.AddYoujiBrowseCount(&youji);err!=nil{
		Json_Message(&c.Controller,1,"添加计数失败")
	}
	Json_Message(&c.Controller,0,"success！")
}
func (c *YoujiController)AddStartCount() {
	id,_:=c.GetInt("id")
	if id==0{
		Json_Message(&c.Controller,1,"id not right")
	}
	youji:=models.Youji{Id:id}
	if err:=models.AddYoujiStarCount(&youji);err!=nil{
		Json_Message(&c.Controller,1,"添加点赞失败")
	}
	Json_Message(&c.Controller,0,"success！")
}